package com.example.opentiktok.service;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import androidx.annotation.RequiresApi;

import com.example.opentiktok.AccessibilityOperator;
import com.example.opentiktok.DouYinAccessibilityOperator;
import com.example.opentiktok.LogUtil;

public class AccessibilityTestService extends AccessibilityService {


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d("wdddd  服务销毁了，无法继续操作------------");
//        unregisterReceiver(otherReceiver);
    }


    /**
     * 服务中断了，可以引导用户重新开启服务
     */
    @Override
    public void onInterrupt() {
        LogUtil.d("wdddd  服务中断了，无法继续操作------------");

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // 此方法是在主线程中回调过来的，所以消息是阻塞执行的
        // 获取包名
        String pkgName = event.getPackageName().toString();
        if (!start) {
            return;
        }

//        LogUtil.d("pkgName=" + pkgName);
        int eventType = event.getEventType();

        switch (eventType) {
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
                AccessibilityOperator operator = null;
                if (pkgName.equals(DouYinAccessibilityOperator.pkgName)) {
                    operator = DouYinAccessibilityOperator.getInstance();
                }
                if (null != operator) {
                    operator.setContext(this);
                    operator.setAccessibilityService(this);
                    LogUtil.d(" event : " + event.getClassName());
                    LogUtil.d(" event : " + kind);
                    operator.setAccessibilityEvent(event);
                    operator.doAction(orderId, start, event.getClassName().toString(), kind, isDouble);

                }
                break;
        }
    }

    public static final String ACTION_WECHAT_VIDEO_RECEIVER = "ACTION_WECHAT_VIDEO_RECEIVER";

    protected boolean start;
    protected long orderId;//订单id
    // 当前四连是不是直播
    protected boolean isLive = false;
    protected boolean isDouble = false;

    private int kind = -1;
    //视频号列表选择作品，最大重试次数
    private int maxNum = 0;


}